package com.studerb.tests.dao;

import java.util.List;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import com.studerb.dao.ServerDao;
import com.studerb.model.Server;

public final class TestServerDao extends AbstractTransactionalDataSourceSpringContextTests {

	protected ServerDao serverDao;

	protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}

	public void setServerDao(ServerDao serverDao) {
		this.serverDao = serverDao;
	}

	public void testCreate() {
		try {
			deleteFromTables(new String[] { "Server" });
			Server server = new Server("some host", 119, 5, null, null, true, false);
			Long id = serverDao.create(server);
			assertNotNull(id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}

	public void testRead() {
		try {
			Server server = new Server("some host", 119, 5, null, null, true, false);
			Long id = serverDao.create(server);
			assertNotNull(id);
			serverDao.flush(); serverDao.clear();
			Server sameServer = serverDao.read(id);
			assertEquals(server, sameServer);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}

	public void testUpdate() {
		try {
			Server server = new Server("some host", 119, 5, null, null, true, false);
			Long id = serverDao.create(server);
			assertNotNull(id);
			server.setHost("new host");
			Server newServer = serverDao.read(id);
			assertNotNull(newServer);
			assertEquals("new host", newServer.getHost());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}

	public void testDelete() {
		try {
			Server server = new Server("some host", 119, 5, null, null, true, false);
			Long id = serverDao.create(server);
			assertNotNull(id);
			serverDao.delete(server);
			Server newServer = serverDao.read(id);
			assertNull(newServer);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}

	public void testDeleteAll() {
		try {
			deleteFromTables(new String[] { "Server" });
			Server server1 = new Server("some host", 119, 5, null, null, true, false);
			Server server2 = new Server("some host2", 119, 5, null, null, false, false);
			Server server3 = new Server("some host3", 119, 5, null, null, false, false);
			Long id1 = serverDao.create(server1);
			Long id2 = serverDao.create(server2);
			Long id3 = serverDao.create(server3);
			assertNotNull(id1);
			assertNotNull(id2);
			assertNotNull(id3);
			serverDao.flush(); serverDao.clear();
			int deletedCount = serverDao.deleteAll();
			assertTrue(deletedCount >= 3);
			serverDao.flush(); serverDao.clear();
			int postCount = serverDao.getCount();
			assertTrue(postCount == 0);	
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}

	public void testGetAll() {
		try {
			deleteFromTables(new String[] { "Server" });
			List<Server> list = serverDao.getAll();
			assertEquals(0, list.size());
			Server server1 = new Server("some host", 119, 5, null, null, true, false);
			Server server2 = new Server("some host2", 119, 5, null, null, false, false);
			Server server3 = new Server("some host3", 119, 5, null, null, false, false);
			Long id1 = serverDao.create(server1);
			logger.debug("id1: " + id1);
			assertNotNull(id1);
			Long id2 = serverDao.create(server2);
			logger.debug("id2: " + id2);
			assertNotNull(id1);
			Long id3 = serverDao.create(server3);
			logger.debug("id3: " + id3);
			assertNotNull(id1);
			serverDao.flush(); serverDao.clear();
			list = serverDao.getAll();
			logger.debug("size of list: " + list.size());
			assertTrue(list.size() == 3);
			for(Server server : list){
				Long newId = server.getId();
				logger.debug(server);
				assertTrue( id1.equals(newId) || id2.equals(newId) || id3.equals(newId) );
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			fail("Should not have caught exception");
		}
	}
}
